package me.ezitku.base.extensions.BroadCastReceiver

import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import com.pawegio.kandroid.powerManager

/**
 * Created by yurqi on 2017/5/30.
 */

/**
 * ئېكران ئۆچكەننى ، يۇرىغاننى نازارەت قىلىش
 */
fun Context.receiverScreenOnOff(action: ScreenOnOffListener.() -> Unit) {
    var listener = ScreenOnOffListener()
    action.invoke(listener)
    createBreceiver(arrayOf(Intent.ACTION_SCREEN_OFF, Intent.ACTION_SCREEN_ON)) {
        if (it.action.contentEquals(Intent.ACTION_SCREEN_OFF)) {
            listener._off?.invoke()
        } else {
            listener._on?.invoke()
        }
    }
}

class ScreenOnOffListener {
    internal var _off: (() -> Unit)? = null
    internal var _on: (() -> Unit)? = null

    fun on(on: () -> Unit) {
        _on = on
    }

    fun off(off: () -> Unit) {
        _off = off
    }

}

val Context.isScreenOn
    get() = powerManager?.isScreenOn
